package com.wjs.gulimall.cart.Controller;

import com.wjs.gulimall.cart.Interceptor.CartInterceptor;
import com.wjs.gulimall.cart.Service.CartService;
import com.wjs.gulimall.cart.vo.Cart;
import com.wjs.gulimall.cart.vo.CartItem;
import com.wjs.gulimall.cart.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.ExecutionException;

@Controller
public class IndexController {

    @Autowired
    private CartService cartService;

    @GetMapping("/cart.html")
    public String cartList(Model model) throws ExecutionException, InterruptedException {
        Cart cart=cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId")Long skuId, @RequestParam("num")Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * 跳转到成功页面
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {

        // 重定向到成功页面，再次查询购物车数据
        CartItem item = cartService.getCartItem(skuId);
        model.addAttribute("item",item);
        return "success";
    }


    /**

     * @需求思路:
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer checked) {
        cartService.checkItem(skuId,checked);
        // 重定向到购物车页面，重新获取购物车数据
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * @需求描述: 系统管理员 购物车组 模块 用户更改购物车中购物项的数量
     */
    @GetMapping("/countItem")
    public String updateItem(@RequestParam("skuId") Long skuId,
                             @RequestParam("num") Integer count) {
        cartService.updateItem(skuId,count);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * @需求描述: 系统管理员 购物车服务 模块 用户删除购物车中的购物项
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/currentCartItem")
    @ResponseBody
    public List<CartItem> getUserCartItems(){
        return cartService.getUserCartItems();
    }
}
